import math
from math import degrees, radians

import random
from random import choice

import pyglet
from pyglet.gl import  *
from pyglet.window import key

import cocos
from cocos.actions import *
from cocos.layer import *
from cocos.scene import *
from cocos.director import director
from cocos.scenes.transitions import *
from cocos.sprite import Sprite
from cocos.particle import *
from cocos.particle_systems import *

import Box2D as box2d

from res import Images
from globalvars import *

class State(object):
    "a state"
    def __init__(self,ent):
        self.ent = ent

    def Enter(self,dt):
        pass
    
    def Execute(self,dt):
        pass
    
    def Exit(self,dt):
        pass

class StateMachine(object):
    "state machine"
    def __init__(self, owner):
        self.owner = owner
        self.currentState = None
        self.previousState = None
        self.globalState = None
    
    def update(self,dt):
        if self.globalState:
            self.globalState.Execute(dt)
        if self.currentState:
            self.currentState.Execute(dt)
        
    def changeState(self, newState,dt=0):
        if self.currentState:
            self.previousState = self.currentState
            self.currentState.Exit(dt)
            del self.currentState
        self.currentState = newState
        self.currentState.Enter(dt)
        
    def revertToPreviousState(self):
        self.changeState(self.previousState)
    
    def isInState(self,state):
        return self.currentState == state
    
        

class Entity(Sprite):
    "Entity + state machine"
    nextValidID = 0
    def __init__(self, world, image,*args, **kwargs):
        "setup sprite, keys"
        super(Entity,self).__init__(image, *args, **kwargs)
        self.world = world
        self.setID()
        self.stateMachine = StateMachine(self)
        
    def setID(self):
        self.id = Entity.nextValidID
        Entity.nextValidID += 1
        
    def update(self):
        pass
          
    
